/*
 * 阔地教育科技有限公司版权所有(codyy.com/codyy.cn)
 * Copyright (c) 2019 Codyy and/or its affiliates. All rights reserved.
 */

package com.eachann.privacypolicy.library

import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.view.LayoutInflater
import android.view.View
import android.widget.Button
import android.widget.TextView

/**
 * 内部类
 * 1.kotlin默认的内部类是静态内部类，不能持有外部类的状态（属性、方法等）
 * 2.给内部类加上inner关键词之后，就会变成非静态内部类，可以访问外部类的属性和方法
 * 3.非静态内部类想访问外部类的属性，可以使用 this@外部类名.外部类属性名 的形式访问
 * 4.非静态内部类可以访问到外部静态内部类的方法和属性，静态内部类访问不到外部所有的属性和方法
 */

class PrivacyPolicyController {

    private lateinit var mView: View
    private var mTitleView: TextView? = null
    private var mMessageView: TextView? = null
    private var mButtonPositive: Button? = null
    private var mButtonNeutral: Button? = null
    private var dialog: Dialog? = null
    private var mTitle: CharSequence? = null
    private var mMessage: CharSequence? = null
    private var cancelable: Boolean = true
    var mOnCancelListener: DialogInterface.OnCancelListener? = null
    var mOnDismissListener: DialogInterface.OnDismissListener? = null
    fun setOnCancelListener(onCancelListener: DialogInterface.OnCancelListener?) {
        mOnCancelListener = onCancelListener
    }

    fun setOnDismissListener(onDismissListener: DialogInterface.OnDismissListener?) {
        mOnDismissListener = onDismissListener
    }

    fun setDialog(di: Dialog) {
        dialog = di
        dialog?.setCanceledOnTouchOutside(cancelable)
    }

    private fun setContentView(
        inflater: LayoutInflater
    ): View {
        if (!this::mView.isInitialized) {
            mView = inflater.inflate(R.layout.layout_privacy_policy_dialog, null, false)
            mTitleView = mView.findViewById(R.id.tv_privacy_policy_dialog_title)
            mMessageView = mView.findViewById(R.id.tv_privacy_policy_dialog_content)
            mButtonNeutral = mView.findViewById(R.id.tv_privacy_policy_dialog_detail)
            mButtonPositive = mView.findViewById(R.id.tv_privacy_policy_dialog_agree)
        }
        setUpView()
        return mView
    }

    private fun setUpView() {
        mTitleView?.text = mTitle
        mMessageView?.text = mMessage
    }

    fun getView() = mView

    fun setTitle(title: CharSequence) {
        mTitle = title
    }

    fun setMessage(message: CharSequence) {
        mMessage = message
    }

    fun setButton(
        whichButton: Int, text: CharSequence?,
        listener: DialogInterface.OnClickListener?
    ) {
        getButton(whichButton)?.visibility = View.VISIBLE
        getButton(whichButton)?.text = text
        getButton(whichButton)?.setOnClickListener {
            listener?.run {
                onClick(dialog, whichButton)
                if(whichButton==DialogInterface.BUTTON_POSITIVE){
                    PrivacyPolicyHelper.saveAgree(it.context)
                }
            }
        }
    }

    private fun getButton(whichButton: Int): Button? = when (whichButton) {
        DialogInterface.BUTTON_POSITIVE -> mButtonPositive
        DialogInterface.BUTTON_NEUTRAL -> mButtonNeutral
        else -> null
    }

    class ModalParams constructor(
        val context: Context
    ) {
        var title: CharSequence? = null
        var message: CharSequence? = null
        var positiveButtonText: CharSequence? = null
        var neutralButtonText: CharSequence? = null
        var positiveButtonListener: DialogInterface.OnClickListener? = null
        var neutralButtonListener: DialogInterface.OnClickListener? = null
        var cancelable: Boolean = true
        var onCancelListener: DialogInterface.OnCancelListener? = null
        var onDismissListener: DialogInterface.OnDismissListener? = null
        var positiveButtonBg = -1
        fun apply(controller: PrivacyPolicyController) {
            title?.let {
                controller.setTitle(it)
            }
            controller.cancelable = cancelable

            message?.let {
                controller.setMessage(it)
            }
            controller.setContentView(LayoutInflater.from(context))
            positiveButtonText?.let {
                controller.setButton(
                    DialogInterface.BUTTON_POSITIVE,
                    positiveButtonText,
                    positiveButtonListener
                )
                if (positiveButtonBg != -1) {
                    controller.getButton(DialogInterface.BUTTON_POSITIVE)?.setBackgroundResource(
                        positiveButtonBg
                    )
                }

            }
            neutralButtonText?.let {
                controller.setButton(
                    DialogInterface.BUTTON_NEUTRAL,
                    neutralButtonText,
                    neutralButtonListener
                )
            }
            onDismissListener?.let {
                controller.setOnDismissListener(onDismissListener)
            }
            onCancelListener?.let {
                controller.setOnCancelListener(onCancelListener)
            }

        }
    }
}

